package com.edward.action;

import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import org.springframework.security.annotation.Secured;

import com.edward.JsonDateProcessor;
import com.edward.JsonTimeProcessor;
import com.edward.PagerModel;
import com.edward.model.FeeList;
import com.edward.service.FeeListManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class FeeListAction extends ActionSupport{


	private static final long serialVersionUID = 4912812398720905688L;

	private FeeList feeList;
	
	private FeeListManager feeListManager;
	
	private String query="";
	
	private int limit;
	
	private int start;
	
	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public FeeList getFeeList() {
		return feeList;
	}

	public void setFeeList(FeeList feeList) {
		this.feeList = feeList;
	}

	public void setFeeListManager(FeeListManager feeListManager) {
		this.feeListManager = feeListManager;
	}

	@Secured("ROLE_COREFINANCE_ADD")
	public String add() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		FeeList f = feeList;
		feeList.setSum(f.getDepreciation()
				+ f.getEduRate() + f.getElecRate() + f.getGather()
				+ f.getInterest() + f.getMeal() + f.getOfficRate()
				+ f.getOperationRate() + f.getOther()
				+ f.getRent()+ f.getTeleRate() + f.getTwRate()
				+ f.getWage() + f.getWaterRate() + f.getSocial() + f.getSport()
				);
		feeListManager.addFeeList(feeList);
		feeListManager.addcheck(feeList);
		response.getWriter().print("{success : true , msg : '添加成功!'}");
		return null;
	}
	
	@Secured("ROLE_COREFINANCE_UPDATE")
	public String update() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		FeeList f = feeList;
		feeList.setSum(f.getDepreciation()
				+ f.getEduRate() + f.getElecRate() + f.getGather()
				+ f.getInterest() + f.getMeal() + f.getOfficRate()
				+ f.getOperationRate() + f.getOther()
				+ f.getRent()+ f.getTeleRate() + f.getTwRate()
				+ f.getWage() + f.getWaterRate() + f.getSocial() + f.getSport()
				);
		FeeList fee = feeListManager.getFeeList(feeList.getId());
		f = feeListManager.getSum(f.getDate(), f.getType());
		f.setDepreciation(f.getDepreciation() + feeList.getDepreciation() - fee.getDepreciation());
		f.setEduRate(f.getEduRate()+ feeList.getEduRate() - fee.getEduRate());
		f.setElecRate(f.getElecRate()+ feeList.getElecRate() - fee.getElecRate());
		f.setGather(f.getGather()+ feeList.getGather() - fee.getGather());
		f.setInterest(f.getInterest()+ feeList.getInterest() - fee.getInterest());
		f.setMeal(f.getMeal()+ feeList.getMeal() - fee.getMeal());
		f.setOfficRate(f.getOfficRate()+ feeList.getOfficRate() - fee.getOfficRate());
		f.setOperationRate(f.getOperationRate()+ feeList.getOperationRate() - fee.getOperationRate());
		f.setOther(f.getOther()+ feeList.getOther() - fee.getOther());
		f.setRent(f.getRent()+ feeList.getRent() - fee.getRent());
		f.setTeleRate(f.getTeleRate()+ feeList.getTeleRate() - fee.getTeleRate());
		f.setTwRate(f.getTwRate()+ feeList.getTwRate() - fee.getTwRate());
		f.setWage(f.getWage()+ feeList.getWage() - fee.getWage());
		f.setWaterRate(f.getWaterRate()+ feeList.getWaterRate() - fee.getWaterRate());
		f.setSport(f.getSport() + feeList.getSport() - fee.getSport());
		f.setSocial(f.getSocial() + feeList.getSocial() - fee.getSocial());
		f.setSum(f.getSum()+ feeList.getSum() - fee.getSum());
		feeListManager.updateFeeList(f);
		feeListManager.updateFeeList(feeList);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_COREFINANCE_UPDATE")
	public String delete() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		FeeList fee = feeListManager.getFeeList(feeList.getId());
		FeeList f = feeListManager.getSum(fee.getDate(), fee.getType());
		f.setDepreciation(f.getDepreciation() - fee.getDepreciation());
		f.setEduRate(f.getEduRate() - fee.getEduRate());
		f.setElecRate(f.getElecRate()- fee.getElecRate());
		f.setGather(f.getGather()- fee.getGather());
		f.setInterest(f.getInterest()- fee.getInterest());
		f.setMeal(f.getMeal()- fee.getMeal());
		f.setOfficRate(f.getOfficRate() - fee.getOfficRate());
		f.setOperationRate(f.getOperationRate() - fee.getOperationRate());
		f.setOther(f.getOther()- fee.getOther());
		f.setRent(f.getRent()- fee.getRent());
		f.setTeleRate(f.getTeleRate()- fee.getTeleRate());
		f.setTwRate(f.getTwRate() - fee.getTwRate());
		f.setWage(f.getWage() - fee.getWage());
		f.setWaterRate(f.getWaterRate() - fee.getWaterRate());
		f.setSocial(f.getSocial() - fee.getSocial());
		f.setSport(f.getSport() - fee.getSport());
		f.setSum(f.getSum() - fee.getSum());
		feeListManager.updateFeeList(f);
		feeListManager.deleteFeeList(fee.getId());
		response.getWriter().print("{success : true , msg : '删除成功!'}");
		return null;
	}

	@Secured("ROLE_COREFINANCE")
	public String getList() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonTimeProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonTimeProcessor());
		PagerModel pager;
	    pager = feeListManager.pagerList(start, limit,query,feeList.getType());
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	@Secured("ROLE_COREFINANCE")
	public String getSumList() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
	    pager = feeListManager.pagerSumList(start, limit,query,feeList.getType());
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	
	@Secured("ROLE_COREFINANCE")
	 public String getListJudge() throws Exception {
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print("{success : true , msg : '成功!',deny:false}");
		 return null;
	 }
	
	
	
	
	
	
	

}
